<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单投诉查询类
 * @package  module/base/bill
 * @author   create by allen.qiang
 * @version 1.0
 */

class billComplain{
    /**
     * 查询维权投诉表信息
     * @param array $arg_array
     * @return null|array
     */
    public function billComplainSearch( &$arg_array ) {

        global $G_PGLOB;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //订单号
        if (isset($arg_array['bill_no']) && !empty($arg_array['bill_no'])) {
            $sql .= ' AND bill_no = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['bill_no'];
        }

        //投诉编号
        if (isset($arg_array['complain_id']) && is_numeric($arg_array['complain_id'])) {
            $sql .= ' AND complain_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['complain_id'];
        }

        //订单编号模糊查询
        if (isset($arg_array['bill_code']) && !empty($arg_array['bill_code'])) {
            $sql .= ' AND bill_code LIKE ? ';
            $params_type .= 's';
            $params_array[] = '%'.$arg_array['bill_code']. '%';
        }

        //会员名
        if( isset($arg_array['cust_name']) && !empty($arg_array['cust_name'])){
            $sql .=' AND cust_name LIKE ? ';
            $params_type .='s';
            $params_array[] ='%'.$arg_array['cust_name'].'%';
        }

        //创建开始时间
        if (isset($arg_array['startDate']) && !empty($arg_array['startDate']) ){
            $sql .= ' AND createTime > ?';
            $params_type .= 's';
            $params_array[] = $arg_array['startDate'] . ' 00:00:00';
        }

        //创建结束时间
        if (isset($arg_array['endDate']) && !empty($arg_array['endDate']) ){
            $sql .= ' AND createTime < ?';
            $params_type .= 's';
            $params_array[] = $arg_array['endDate'] . ' 23:59:59';
        }

        //处理开始时间
        if (isset($arg_array['auditStartDate']) && !empty($arg_array['auditStartDate']) ){
            $sql .= ' AND complain_dispose_time > ?';
            $params_type .= 's';
            $params_array[] = $arg_array['auditStartDate'] . ' 00:00:00';
        }

        //处理结束时间
        if (isset($arg_array['auditEndDate']) && !empty($arg_array['auditEndDate']) ){
            $sql .= ' AND complain_dispose_time < ?';
            $params_type .= 's';
            $params_array[] = $arg_array['auditEndDate'] . ' 23:59:59';
        }

        //投诉状态
        if( isset($arg_array['complain_status']) && is_numeric($arg_array['complain_status'])){
            $sql .=' AND complain_status = ? ';
            $params_type .='i';
            $params_array[] =$arg_array['complain_status'];
        }

        //拓展字段
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM bill_complain' . $sql;

                $count_array = &$G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);

                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                if($_REQUEST['count'] < $_REQUEST['rp']){
                    $_REQUEST['page'] = 1;
                }

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM bill_complain' . $sql;

            $temp_item_array = $G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);

            if (!$temp_item_array) {
                return null;
            }

            foreach ($temp_item_array as $item) {

                //查询投诉图片
                $complain_id = $item->complain_id;
                $img_sql = 'SELECT * FROM bill_complain_image WHERE complain_id = '.$complain_id;
                $img_array = $G_PGLOB->DBCA->stmt_query( $img_sql );
                if( $img_array ){
                    foreach( $img_array as $val ){
                        $temp_array[] = array(
                            'complain_id' => $val->complain_id,
                            'image_link' =>  $val->image_link ? G_IMAGE_URL . $val->image_link : '' ,
                            'sort' => $val->sort
                        );
                    }
                }
                $item_array[] = array(

                    'bill_no' => (string)$item->bill_no,
                    'bill_code' => (string)$item->bill_code,
                    'creator' => $item->creator,
                    'createTime' => $item->createTime,
                    'createDate' => date( 'Y-m-d',strtotime($item->createTime) ),
                    'tendayFeturn'=>date("Y-m-d",strtotime("+10 day")),
                    'complain_id' => $item->complain_id,
                    'complain_content' => $item->complain_content,
                    'complain_status' => $item->complain_status,
                    'mobile' => $item->mobile,
                    'phone' => $item->phone,
                    'cust_name' => $item->cust_name,
                    'complain_dispose_person' => $item->complain_dispose_person,
                    'complain_dispose_time' => $item->complain_dispose_time,
                    'complain_dispose_remark' => convert_var($item->complain_dispose_remark),
                    'img_array'=>$temp_array
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;
        }

    }

    /**
     * 保存维权投诉表信息
     * @param array $arg_array
     * @return null|array
     */
    public function saveBillComplain( $args_array ) {

        //引用文件上传插件文件
        quanli_include('module/base/common/quanliFile.class.php');

        global $G_SHOP;

        if (!isset($args_array['bill_no'], $args_array['complain_content'],
            $args_array['operator'], $args_array['pcustID'],$args_array['bill_code'])
            || empty($args_array['bill_no']) || empty($args_array['operator']) || !quanli_is_int($args_array['pcustID'])
            || empty($args_array['complain_content']) || empty( $args_array['bill_code'] ) ) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            $G_SHOP->DBCA->beginTransaction();

            //查询订单信息
            $bill_args_array = array(
                'bill_no' => $args_array['bill_no']
            );
            $bill_array = $this->billComplainSearch( $bill_args_array );
            if ($bill_array) {
                throw new Exception('你已经投诉，无需再次进行投诉', 404);
            }

            $bill_complain = new entity('bill_complain');
            $bill_complain->creator = $args_array['operator'];
            $bill_complain->complain_id = generate_seqno('seqno_bill_complain');
            $bill_complain->complain_content = $args_array['complain_content'];

            $bill_complain->complain_status = 1;
            $bill_complain->bill_no = $args_array['bill_no'];
            $bill_complain->bill_code = $args_array['bill_code'];

            $bill_complain->mobile = $args_array['mobile'];
            $bill_complain->phone = $args_array['phone'];
            $bill_complain->cust_name = $args_array['cust_name'];
            $G_SHOP->DBCA->save( $bill_complain );

            if( isset( $args_array['upload_images'] ) && !empty( $args_array['upload_images'] )){
                foreach( $args_array['upload_images'] as $k=>$v){
                    $bill_complain_image = new entity('bill_complain_image');

                    //实例文件类
                    $quanliFile = new quanliFile();

                    //生成新地址
                    $args_array['image_link'] = $quanliFile->basename_db($v, 'complain');

                    $bill_complain_image->creator = $args_array['operator'];
                    $bill_complain_image->complain_id = $bill_complain->complain_id;
                    $bill_complain_image->image_link = $args_array['image_link'];
                    $bill_complain_image->sort = $k;
                    $G_SHOP->DBCA->save( $bill_complain_image );
                }
            }


            $G_SHOP->DBCA->commit();

            return array('code'=> 200, 'message'=> '投诉维权信息提交成功');

        } catch (Exception $e) {

            $G_SHOP->DBCA->rollBack();

            if ($e->getCode() > 0){
                return array('code'=> $e->getCode(), 'message'=> $e->getMessage());
            }else {
                error_log($e->getMessage());
                return array('code' => 500, 'message'=> '系统错误');
            }

        }

    }
}
